from rest_framework.viewsets import ModelViewSet
from goods.models import SPUSpecification
from .serializers import SPUSpecificationSerializer
from meiduo_admin.utils import MyNumbPage
from rest_framework.decorators import action
from rest_framework.response import Response


class SpecsViewSet(ModelViewSet):
    serializer_class = SPUSpecificationSerializer

    pagination_class = MyNumbPage

    def get_queryset(self):
        queryset = SPUSpecification.objects.all()
        for spec in queryset:
            spec.name = '{}-{}'.format(spec.spu.name, spec.name)
        return queryset

    # goods/specs/simple
    @action(methods=['get'], detail=False)
    def simple(self, request):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
